import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  RoleApi,
  RoleReq,
  RolePage,
  RoleCreateReq,
  RoleUpdateReq,
  VerifyRoleReq,
  RoleModel,
  RoleKey,
  RoleKeyList,
  RoleMoveKey,
} from "./types";

class RoleApiImpl implements RoleApi {
  getKey(model: RoleModel): RoleKey {
    return {
      roleId: model.roleId,
    } as RoleKey;
  }
  /**
   * 获取页面数据
   *
   * @param queryParams 查询参数，可选
   * @returns 返回请求结果
   */
  findPage(query: RoleReq): AxiosPromise<RolePage> {
    if (query) {
      query.hasRoleType = query.roleType != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/rbac/role/list?tenant=" + useUserStore().user.tenant,
      method: "get",
      params: query,
    });
  }

  /**
   * 删除角色
   *
   * @param roleId 角色ID
   * @returns 返回请求结果
   */
  delete(query: RoleKey): AxiosPromise<RoleModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/role/id",
      method: "delete",
      params: query,
    });
  }

  /**
   * 批量删除角色
   *
   * @param ids 角色ID数组
   * @returns 返回请求结果
   */
  batchDelete(keys: RoleKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.roleId);
    const data: RoleKeyList = {
      tenant: useUserStore().user.tenant,
      roleId: ids,
    } as RoleKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/role/batch",
      method: "put",
      data: data,
    });
  }

  /**
   * 编辑角色
   *
   * @param data 编辑角色的数据
   * @returns 返回请求结果
   */
  update(data: RoleUpdateReq): AxiosPromise<RoleModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/role",
      method: "patch",
      data: data,
    });
  }

  /**
   * 添加角色
   *
   * @param data 角色数据
   * @returns 返回请求结果
   */
  create(data: RoleCreateReq): AxiosPromise<RoleModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/role",
      method: "post",
      data: data,
    });
  }

  /**
   * 向上/下移动角色
   *
   * @param roleId 角色ID
   * @returns 返回Promise对象，表示异步操作的结果
   */
  move(key: RoleKey, up: boolean): AxiosPromise<boolean> {
    const data: RoleMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/role/move",
      method: "put",
      data: data,
    });
  }

  // 查询角色/职务表
  findById(query: RoleKey): AxiosPromise<RoleModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/role/id",
      method: "get",
      params: query,
    });
  }

  /**
   * 根据角色ID获取角色信息
   *
   * @param roleId 角色ID
   * @returns 返回请求结果
   */
  roleId(roleId: string) {
    const query = {
      roleId: roleId,
    };
    return this.findById(query);
  }

  /**
   * 获取树形列表的角色信息
   *
   * @param roleId 角色ID，可选
   * @returns 返回请求结果
   */
  treelistRole(roleId?: string): AxiosPromise {
    const query = {
      tenant: useUserStore().user.tenant,
      roleType: "role",
      hasRoleType: true,
      excludeId: roleId,
    };
    return request({
      url: "/v1/rbac/role/treelist",
      method: "get",
      params: query,
    });
  }

  /**
   * 判断角色是否存在
   *
   * @param code 角色代码
   * @returns 返回角色是否存在的结果
   */
  existRole(query: VerifyRoleReq): AxiosPromise {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/role/exist",
      method: "get",
      params: query,
    });
  }
}

const roleApi = new RoleApiImpl();
export { roleApi };
